package com.pikaqiu.controller.backend;

import com.pikaqiu.common.Const;
import com.pikaqiu.common.ResponseCode;
import com.pikaqiu.common.ServerResponse;
import com.pikaqiu.pojo.Category;
import com.pikaqiu.pojo.User;
import com.pikaqiu.service.ICategoryService;
import com.pikaqiu.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashSet;

/**
 * Created by Administrator on 2018/6/11.
 */
@RequestMapping("/managerCategory")
@Controller
public class CategoryManagerController{

    @Autowired
    private IUserService userService;

    @Autowired
    private ICategoryService iCategoryService;

    @RequestMapping("/addCategory")
    @ResponseBody
    public ServerResponse addCategory(HttpSession session, String categoryName, @RequestParam(value = "parentId", defaultValue = "0") int parentId) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录");
        }
        //是否是管理员
        if(userService.checkUserRole(user)){
            return iCategoryService.addCategory(categoryName, parentId);
        }else {
            return ServerResponse.createByErrorMessage("无权限操作");
        }
    }

    @RequestMapping
    @ResponseBody
    public ServerResponse setCategoryName(HttpSession session,String categoryName,Integer CategoryId){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录");
        }
        //是否是管理员
        if(userService.checkUserRole(user)){
            return iCategoryService.updateCategoryName(CategoryId, categoryName);
        }else {
            return ServerResponse.createByErrorMessage("无权限操作");
        }
    }

    @RequestMapping("/getChildrenParallelCategory")
    @ResponseBody
    public ServerResponse getChildrenParallelCategory(HttpSession session, @RequestParam(value = "categoryId", defaultValue = "0") Integer categoryId) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录");
        }
        //是否是管理员
        if(userService.checkUserRole(user)){
            return iCategoryService.ChildrenParallel(categoryId);
        }else {
            return ServerResponse.createByErrorMessage("无权限操作");
        }
    }
    @RequestMapping("/getAllCategory")
    @ResponseBody
    public ServerResponse getAllCategory(HttpSession session, @RequestParam(value = "categoryId", defaultValue = "0") Integer categoryId) {

        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录");
        }
        //是否是管理员
        if (userService.checkUserRole(user)) {
            return iCategoryService.AllChildrenParallel(new HashSet<Category>(),categoryId);
        } else {
            return ServerResponse.createByErrorMessage("无权限操作");
        }
    }
}
